//
//  HoneyAndStoreViewController.m
//  shopping
//
//  Created by 徐双 on 2017/9/13.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HoneyAndStoreViewController.h"
#import "HoneyViewController.h"
#import "StoreViewController.h"

@interface HoneyAndStoreViewController ()<ZJScrollPageViewDelegate>

@property(strong, nonatomic)NSArray<NSString *> *titles;
@property(strong, nonatomic)NSArray<UIViewController<ZJScrollPageViewChildVcDelegate> *> *childVcs;

@property (weak, nonatomic) ZJContentView *contentView;

@property (weak, nonatomic) ZJScrollSegmentView *segmentView;
@end

@implementation HoneyAndStoreViewController

- (void)viewWillAppear:(BOOL)animated{
    
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = nil;
    UIButton *btn1 =[[UIButton alloc]initWithFrame:CGRectMake(0,0, 22, 22)];
    
    [btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setBackgroundImage:[[UIImage imageNamed:@"back_black"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn1];
    self.navigationItem.leftBarButtonItem  = item;
    //必要的设置, 如果没有设置可能导致内容显示不正常
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    self.childVcs = [self setupChildVc];
    
    
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    style.showCover = YES;
    // 不要滚动标题, 每个标题将平分宽度
    style.scrollTitle = YES;
    // 渐变
    style.gradualChangeTitleColor = YES;
    style.coverCornerRadius = 0;
    // 遮盖背景颜色
    style.coverBackgroundColor = [UIColor clearColor];
    //标题一般状态颜色 --- 注意一定要使用RGB空间的颜色值
    style.normalTitleColor = getColor(@"222222");
    
    style.titleMargin = 50;
    
    //标题选中状态颜色 --- 注意一定要使用RGB空间的颜色值
    style.selectedTitleColor = getColor(@"222222");
    style.scrollLineColor = getColor(@"222222");
    style.titleFont = [UIFont systemFontOfSize:18];
    
    style.showCover = YES;
    style.showLine = YES;
    
    self.titles = @[@"宝贝", @"店铺"];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    // 注意: 一定要避免循环引用!!
    __weak typeof(self) weakSelf = self;
    ZJScrollSegmentView *segment = [[ZJScrollSegmentView alloc] initWithFrame:CGRectMake(0, 64.0, 250, 28.0) segmentStyle:style delegate:self titles:self.titles titleDidClick:^(ZJTitleView *titleView, NSInteger index) {
        
        [weakSelf.contentView setContentOffSet:CGPointMake(weakSelf.contentView.bounds.size.width * index, 0.0) animated:YES];
        
    }];
    
    self.segmentView = segment;
    self.navigationItem.titleView = self.segmentView;
    
  
    ZJContentView *content = [[ZJContentView alloc] initWithFrame:CGRectMake(0.0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 44.0) segmentView:self.segmentView parentViewController:self delegate:self];
    //    NSLog(@"=========%.2f , %.2f" , self.view.bounds.size.width , self.view.bounds.size.height);
    self.contentView = content;
    [self.view addSubview:self.contentView];
    
    
}

-(void)onClick:(UIButton *)btn{

    [self.navigationController popViewControllerAnimated:YES];

}
#pragma mark ----------------ZJScrollPageViewDelegate---------------------
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (NSArray *)setupChildVc {
    
    HoneyViewController *vc1 = [HoneyViewController new];
    
    StoreViewController *vc2 = [StoreViewController new];
    
    
    
    NSArray *childVcs = [NSArray arrayWithObjects:vc1, vc2, nil];
    return childVcs;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = self.childVcs[index];
    }
    
    
    
    
    return childVc;
}

@end
